package net.zjitc.android2019.ch3storage;

import android.content.Context;
import android.os.Environment;
import android.support.annotation.NonNull;
import android.widget.Toast;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.util.HashMap;


public class FileSPSaveQQ {

    static File file;
    public static HashMap<String,String> get(Context context) {
        HashMap<String,String> hashMap = new HashMap<String,String>();
        String device = Environment.getExternalStorageState();
        if (device.equals(Environment.MEDIA_MOUNTED)){
            File path=Environment.getExternalStorageDirectory();
        }
        FileInputStream in;
        BufferedReader br;

        try {
            in=new FileInputStream(file);
            br=new BufferedReader(new InputStreamReader(in));

            hashMap.put("name",br.readLine());
            hashMap.put("pwd",br.readLine());
        } catch (Exception e) {
            e.printStackTrace();
        }

        return hashMap;
    }

    public static boolean sava(Context context, String name, String pwd) {
        String device =Environment.getExternalStorageState();
        if (device.equals(Environment.MEDIA_MOUNTED)){
            File path = Environment.getExternalStorageDirectory();

            file = new File(path,"wyj.txt");

            FileOutputStream out = null;
            BufferedWriter bw = null;
            try {
                out=new FileOutputStream(file);
                bw = new BufferedWriter(new OutputStreamWriter(out));
                bw.write(name+"\n"+pwd);
                bw.flush();
                Toast.makeText(context,"写入成功",Toast.LENGTH_SHORT).show();
            } catch (Exception e) {
                e.printStackTrace();
            }

        }
        return true;
    }
}
